<?php
require 'vendor/autoload.php';

$app = new \Slim\Slim();

$app->get('/', function() use ($app) {
    $app->render('grid.php');
});

$app->post('/', function() use ($app) {
    $data = array();
    $downloads = array();
    $keyword = $app->request->post('s');
    $client = new Packagist\Api\Client();
    foreach ($client->search($keyword) as $result) {
        $data[] = array(
            'name'          => $result->getName(),
            'description'   => $result->getDescription(),
            'url'           => $result->getUrl(),
            'downloads'     => $result->getDownloads()
        );
        $downloads[] = $result->getDownloads();
    }
    array_multisort($downloads, SORT_DESC, $data);

    $app->render('grid.php', array(
        'data'      => $data,
        'keyword'   => $keyword
    ));
});

$app->run();